package com.example.communitysystem.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletResponse;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    private SessionInterceptor sessionInterceptor;

    public void addInterceptors(InterceptorRegistry registry){
        //拦截根目录下的所有请求
        registry.addInterceptor(sessionInterceptor).addPathPatterns("/**");
    }

    //处理静态资源访问
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
            addResourceHandler是访问路径前缀，参数可设置多个，以逗号分隔，例如：/image/**,/css/**
            addResourceLocations是对应的资源路径，也可以设置多个
         */
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    @Autowired
    public void setSessionInterceptor(SessionInterceptor sessionInterceptor) {
        this.sessionInterceptor = sessionInterceptor;
    }
}
